/*
 * NetworkOptionsPanel.java
 *
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Lailaps.
 *
 * Tiberiumlabs Lailaps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Lailaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.tiberiumlabs.lailaps.settings.gui;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.ResourceBundle;
import javax.swing.AbstractListModel;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.tiberiumlabs.lailaps.Mediator;
import org.tiberiumlabs.lailaps.settings.NetworkSettings;
import org.tiberiumlabs.lailaps.settings.NetworkSettingsImpl;
import org.tiberiumlabs.lailaps.settings.Settings;
import org.tiberiumlabs.lailaps.settings.helpers.UserAgent;
import org.tiberiumlabs.lailaps.settings.helpers.UserAgentComboBoxModel;
import org.tiberiumlabs.lailaps.threads.ThreadsMap;
import org.tiberiumlabs.lailaps.toolkit.Constants;

/**
 *
 * @author <a href="mailto:beast.tiberiumlabs@gmail.com">Beast</a>
 */
public class NetworkOptionsPanel extends javax.swing.JPanel implements OptionPanel {
    
    NetworkSettings netSettings;
    NetworkSettingsImpl netSettingsWriter;
    
    private ResourceBundle bundle = ResourceBundle.getBundle("org/tiberiumlabs/lailaps/resources/messages/settings_network");
    private EncodingListModel encodingListModel = new EncodingListModel();
    
    /** Creates new form NetworkOptionsPanel */
    public NetworkOptionsPanel() {
        initComponents();
        netSettings = Settings.getNetworkSettings();
        UserAgentComboBoxModel userAgentModel = new UserAgentComboBoxModel();
        userAgentComboBox.setModel(userAgentModel);
        readGeneralSettings();
        readHTTPSettings();
        readProxySettings();
        readFTPSettings();
        
        encodingsList.setSelectedIndex(0);
        
        encodingComboBox.addItem(bundle.getString("selectEncoding.string"));
        Map<String, Charset> charsetMap = Charset.availableCharsets();
        Iterator<String> iterator = charsetMap.keySet().iterator();
        while (iterator.hasNext()) {
            encodingComboBox.addItem(iterator.next());
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {

        ftpModeGroup = new javax.swing.ButtonGroup();
        ftpTransTypeGroup = new javax.swing.ButtonGroup();
        proxyGroup = new javax.swing.ButtonGroup();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        generalPanel = new javax.swing.JPanel();
        bufferSizeLabel = new javax.swing.JLabel();
        bufferSpinner = new javax.swing.JSpinner();
        simDownloadsLabel = new javax.swing.JLabel();
        simDownloadsSpinner = new javax.swing.JSpinner();
        encodingLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        encodingsList = new javax.swing.JList();
        encodingComboBox = new javax.swing.JComboBox();
        moveUpButton = new javax.swing.JButton();
        moveDownButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        httpPanel = new javax.swing.JPanel();
        userAgentLabel = new javax.swing.JLabel();
        userAgentComboBox = new javax.swing.JComboBox();
        timeoutLabel = new javax.swing.JLabel();
        timeoutSpinner = new javax.swing.JSpinner();
        useCustomAgentCheckBox = new javax.swing.JCheckBox();
        customAgentField = new javax.swing.JTextField();
        ftpPanel = new javax.swing.JPanel();
        dataTimeoutLabel = new javax.swing.JLabel();
        modePanel = new javax.swing.JPanel();
        activeModeRadioButton = new javax.swing.JRadioButton();
        passiveModeRadioButton = new javax.swing.JRadioButton();
        dataTimeoutSpinner = new javax.swing.JSpinner();
        transferTypePanel = new javax.swing.JPanel();
        binaryTypeRadioButton = new javax.swing.JRadioButton();
        asciiTypeRadioButton = new javax.swing.JRadioButton();
        ebcdicTypeRadioButton = new javax.swing.JRadioButton();
        binaryImageTypeRadioButton = new javax.swing.JRadioButton();
        miscPanel = new javax.swing.JPanel();
        tcpNoDelayCheckBox = new javax.swing.JCheckBox();
        proxyPanel = new javax.swing.JPanel();
        httpProxyRadioButton = new javax.swing.JRadioButton();
        proxyHostLabel = new javax.swing.JLabel();
        noProxyForLabel = new javax.swing.JLabel();
        portLabel = new javax.swing.JLabel();
        proxyNeedAuthCheckBox = new javax.swing.JCheckBox();
        loginLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        socksRadioButton = new javax.swing.JRadioButton();
        exampleLabel = new javax.swing.JLabel();
        useProxyCheckBox = new javax.swing.JCheckBox();
        proxyHostTextField = new javax.swing.JTextField();
        noProxyForTextField = new javax.swing.JTextField();
        portTextField = new javax.swing.JTextField();
        loginTextField = new javax.swing.JTextField();
        passwordField = new javax.swing.JPasswordField();

        jTabbedPane1.setFont(new java.awt.Font("Dialog", 0, 12));
        jTabbedPane1.setPreferredSize(new java.awt.Dimension(0, 0));

        bufferSizeLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        bufferSizeLabel.setText(bundle.getString("bufferSizeLabel")); // NOI18N

        simDownloadsLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        simDownloadsLabel.setText(bundle.getString("simDownloadsLabel")); // NOI18N

        encodingLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        encodingLabel.setText(bundle.getString("encodingLabel")); // NOI18N

        encodingsList.setModel(encodingListModel);
        encodingsList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                encodingsListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(encodingsList);

        encodingComboBox.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                encodingComboBoxItemStateChanged(evt);
            }
        });

        moveUpButton.setFont(new java.awt.Font("Dialog", 0, 12));
        moveUpButton.setText(bundle.getString("moveUpButton")); // NOI18N
        moveUpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveUpButtonActionPerformed(evt);
            }
        });

        moveDownButton.setFont(new java.awt.Font("Dialog", 0, 12));
        moveDownButton.setText(bundle.getString("moveDownButton")); // NOI18N
        moveDownButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moveDownButtonActionPerformed(evt);
            }
        });

        removeButton.setFont(new java.awt.Font("Dialog", 0, 12));
        removeButton.setText(bundle.getString("removeButton")); // NOI18N
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        addButton.setFont(new java.awt.Font("Dialog", 0, 12));
        addButton.setText(bundle.getString("addButton")); // NOI18N
        addButton.setEnabled(false);
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout generalPanelLayout = new javax.swing.GroupLayout(generalPanel);
        generalPanel.setLayout(generalPanelLayout);
        generalPanelLayout.setHorizontalGroup(
            generalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(generalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(generalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(generalPanelLayout.createSequentialGroup()
                        .addGroup(generalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(bufferSizeLabel)
                            .addComponent(simDownloadsLabel))
                        .addGap(16, 16, 16)
                        .addGroup(generalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(bufferSpinner)
                            .addComponent(simDownloadsSpinner, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)))
                    .addGroup(generalPanelLayout.createSequentialGroup()
                        .addGroup(generalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(encodingLabel)
                            .addComponent(encodingComboBox, javax.swing.GroupLayout.Alignment.TRAILING, 0, 246, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(generalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(moveDownButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(removeButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(moveUpButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(addButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addGap(18, 18, 18))
        );
        generalPanelLayout.setVerticalGroup(
            generalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(generalPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(generalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(simDownloadsLabel)
                    .addComponent(simDownloadsSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(generalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bufferSizeLabel)
                    .addComponent(bufferSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(encodingLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(generalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(generalPanelLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, 0, 0, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(generalPanelLayout.createSequentialGroup()
                        .addComponent(moveUpButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(moveDownButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(removeButton)
                        .addGap(27, 27, 27)))
                .addGroup(generalPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(encodingComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addButton))
                .addContainerGap())
        );

        jTabbedPane1.addTab(bundle.getString("generalPanel.title"), generalPanel); // NOI18N

        userAgentLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        userAgentLabel.setText(bundle.getString("userAgentLabel")); // NOI18N

        userAgentComboBox.setFont(new java.awt.Font("Dialog", 0, 12));

        timeoutLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        timeoutLabel.setText(bundle.getString("timeoutLabel")); // NOI18N

        useCustomAgentCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        useCustomAgentCheckBox.setText(bundle.getString("useCustomAgentCheckBox")); // NOI18N
        useCustomAgentCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        useCustomAgentCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        useCustomAgentCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                useCustomAgentCheckBoxActionPerformed(evt);
            }
        });

        customAgentField.setEnabled(false);

        javax.swing.GroupLayout httpPanelLayout = new javax.swing.GroupLayout(httpPanel);
        httpPanel.setLayout(httpPanelLayout);
        httpPanelLayout.setHorizontalGroup(
            httpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(httpPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(httpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(userAgentLabel)
                    .addComponent(timeoutLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(httpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(useCustomAgentCheckBox)
                    .addComponent(userAgentComboBox, 0, 321, Short.MAX_VALUE)
                    .addComponent(customAgentField, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
                    .addGroup(httpPanelLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(timeoutSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        httpPanelLayout.setVerticalGroup(
            httpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(httpPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(httpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(userAgentLabel)
                    .addComponent(userAgentComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(useCustomAgentCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(customAgentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22)
                .addGroup(httpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(timeoutLabel)
                    .addComponent(timeoutSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(122, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab(bundle.getString("httpPanel.title"), httpPanel); // NOI18N

        dataTimeoutLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        dataTimeoutLabel.setText(bundle.getString("dataTimeoutLabel")); // NOI18N

        modePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("modePanel"))); // NOI18N

        ftpModeGroup.add(activeModeRadioButton);
        activeModeRadioButton.setFont(new java.awt.Font("Dialog", 0, 12));
        activeModeRadioButton.setText(bundle.getString("activeModeRadioButton")); // NOI18N
        activeModeRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        activeModeRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        ftpModeGroup.add(passiveModeRadioButton);
        passiveModeRadioButton.setFont(new java.awt.Font("Dialog", 0, 12));
        passiveModeRadioButton.setSelected(true);
        passiveModeRadioButton.setText(bundle.getString("passiveModeRadioButton")); // NOI18N
        passiveModeRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        passiveModeRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout modePanelLayout = new javax.swing.GroupLayout(modePanel);
        modePanel.setLayout(modePanelLayout);
        modePanelLayout.setHorizontalGroup(
            modePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(modePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(modePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(activeModeRadioButton)
                    .addComponent(passiveModeRadioButton))
                .addContainerGap(121, Short.MAX_VALUE))
        );
        modePanelLayout.setVerticalGroup(
            modePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(modePanelLayout.createSequentialGroup()
                .addComponent(activeModeRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(passiveModeRadioButton)
                .addContainerGap(79, Short.MAX_VALUE))
        );

        transferTypePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("transferTypePanel"))); // NOI18N

        ftpTransTypeGroup.add(binaryTypeRadioButton);
        binaryTypeRadioButton.setFont(new java.awt.Font("Dialog", 0, 12));
        binaryTypeRadioButton.setSelected(true);
        binaryTypeRadioButton.setText(bundle.getString("binaryTypeRadioButton")); // NOI18N
        binaryTypeRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        binaryTypeRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        ftpTransTypeGroup.add(asciiTypeRadioButton);
        asciiTypeRadioButton.setFont(new java.awt.Font("Dialog", 0, 12));
        asciiTypeRadioButton.setText(bundle.getString("asciiTypeRadioButton")); // NOI18N
        asciiTypeRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        asciiTypeRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        ftpTransTypeGroup.add(ebcdicTypeRadioButton);
        ebcdicTypeRadioButton.setFont(new java.awt.Font("Dialog", 0, 12));
        ebcdicTypeRadioButton.setText(bundle.getString("ebcdicTypeRadioButton")); // NOI18N
        ebcdicTypeRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        ebcdicTypeRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        ftpTransTypeGroup.add(binaryImageTypeRadioButton);
        binaryImageTypeRadioButton.setFont(new java.awt.Font("Dialog", 0, 12));
        binaryImageTypeRadioButton.setText(bundle.getString("binaryImageTypeRadioButton")); // NOI18N
        binaryImageTypeRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        binaryImageTypeRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout transferTypePanelLayout = new javax.swing.GroupLayout(transferTypePanel);
        transferTypePanel.setLayout(transferTypePanelLayout);
        transferTypePanelLayout.setHorizontalGroup(
            transferTypePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(transferTypePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(transferTypePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(binaryTypeRadioButton)
                    .addComponent(asciiTypeRadioButton)
                    .addComponent(ebcdicTypeRadioButton)
                    .addComponent(binaryImageTypeRadioButton))
                .addContainerGap(86, Short.MAX_VALUE))
        );
        transferTypePanelLayout.setVerticalGroup(
            transferTypePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(transferTypePanelLayout.createSequentialGroup()
                .addComponent(binaryTypeRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(binaryImageTypeRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(asciiTypeRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ebcdicTypeRadioButton)
                .addContainerGap(33, Short.MAX_VALUE))
        );

        miscPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("miscPanel"))); // NOI18N

        tcpNoDelayCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        tcpNoDelayCheckBox.setSelected(true);
        tcpNoDelayCheckBox.setText(bundle.getString("tcpNoDelayCheckBox")); // NOI18N
        tcpNoDelayCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        tcpNoDelayCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));

        javax.swing.GroupLayout miscPanelLayout = new javax.swing.GroupLayout(miscPanel);
        miscPanel.setLayout(miscPanelLayout);
        miscPanelLayout.setHorizontalGroup(
            miscPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(miscPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tcpNoDelayCheckBox)
                .addContainerGap(184, Short.MAX_VALUE))
        );
        miscPanelLayout.setVerticalGroup(
            miscPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(miscPanelLayout.createSequentialGroup()
                .addComponent(tcpNoDelayCheckBox)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout ftpPanelLayout = new javax.swing.GroupLayout(ftpPanel);
        ftpPanel.setLayout(ftpPanelLayout);
        ftpPanelLayout.setHorizontalGroup(
            ftpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ftpPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ftpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ftpPanelLayout.createSequentialGroup()
                        .addGroup(ftpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(ftpPanelLayout.createSequentialGroup()
                                .addComponent(dataTimeoutLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(dataTimeoutSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(modePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(transferTypePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(miscPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        ftpPanelLayout.setVerticalGroup(
            ftpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ftpPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ftpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dataTimeoutLabel)
                    .addComponent(dataTimeoutSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(ftpPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(modePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(transferTypePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(miscPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab(bundle.getString("ftpPanel.title"), ftpPanel); // NOI18N

        proxyGroup.add(httpProxyRadioButton);
        httpProxyRadioButton.setFont(new java.awt.Font("Dialog", 0, 12));
        httpProxyRadioButton.setSelected(true);
        httpProxyRadioButton.setText(bundle.getString("httpProxyRadioButton")); // NOI18N
        httpProxyRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        httpProxyRadioButton.setEnabled(false);
        httpProxyRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        proxyHostLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        proxyHostLabel.setText(bundle.getString("proxyHostLabel")); // NOI18N

        noProxyForLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        noProxyForLabel.setText(bundle.getString("noProxyForLabel")); // NOI18N

        portLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        portLabel.setText(bundle.getString("portLabel")); // NOI18N

        proxyNeedAuthCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        proxyNeedAuthCheckBox.setText(bundle.getString("proxyNeedAuthCheckBox")); // NOI18N
        proxyNeedAuthCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        proxyNeedAuthCheckBox.setEnabled(false);
        proxyNeedAuthCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        proxyNeedAuthCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                proxyNeedAuthCheckBoxActionPerformed(evt);
            }
        });

        loginLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        loginLabel.setText(bundle.getString("loginLabel")); // NOI18N

        passwordLabel.setFont(new java.awt.Font("Dialog", 0, 12));
        passwordLabel.setText(bundle.getString("passwordLabel")); // NOI18N

        proxyGroup.add(socksRadioButton);
        socksRadioButton.setFont(new java.awt.Font("Dialog", 0, 12));
        socksRadioButton.setText(bundle.getString("socksRadioButton")); // NOI18N
        socksRadioButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        socksRadioButton.setEnabled(false);
        socksRadioButton.setMargin(new java.awt.Insets(0, 0, 0, 0));

        exampleLabel.setFont(new java.awt.Font("Dialog", 0, 10));
        exampleLabel.setText(bundle.getString("exampleLabel")); // NOI18N

        useProxyCheckBox.setFont(new java.awt.Font("Dialog", 0, 12));
        useProxyCheckBox.setText(bundle.getString("useProxyCheckBox")); // NOI18N
        useProxyCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        useProxyCheckBox.setMargin(new java.awt.Insets(0, 0, 0, 0));
        useProxyCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                useProxyCheckBoxActionPerformed(evt);
            }
        });

        proxyHostTextField.setEnabled(false);

        noProxyForTextField.setEnabled(false);

        portTextField.setEnabled(false);
        portTextField.setMinimumSize(new java.awt.Dimension(20, 19));

        loginTextField.setEnabled(false);

        passwordField.setEnabled(false);

        javax.swing.GroupLayout proxyPanelLayout = new javax.swing.GroupLayout(proxyPanel);
        proxyPanel.setLayout(proxyPanelLayout);
        proxyPanelLayout.setHorizontalGroup(
            proxyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(proxyPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(proxyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(httpProxyRadioButton)
                    .addComponent(socksRadioButton)
                    .addComponent(useProxyCheckBox)
                    .addGroup(proxyPanelLayout.createSequentialGroup()
                        .addGroup(proxyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(noProxyForLabel)
                            .addComponent(proxyHostLabel))
                        .addGap(6, 6, 6)
                        .addGroup(proxyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, proxyPanelLayout.createSequentialGroup()
                                .addComponent(proxyHostTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(portLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(portTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addComponent(exampleLabel)
                            .addComponent(noProxyForTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 344, Short.MAX_VALUE)))
                    .addGroup(proxyPanelLayout.createSequentialGroup()
                        .addGap(89, 89, 89)
                        .addGroup(proxyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(proxyNeedAuthCheckBox)
                            .addGroup(proxyPanelLayout.createSequentialGroup()
                                .addGroup(proxyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(passwordLabel)
                                    .addComponent(loginLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(proxyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(loginTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE)
                                    .addComponent(passwordField, javax.swing.GroupLayout.DEFAULT_SIZE, 267, Short.MAX_VALUE))))))
                .addContainerGap())
        );
        proxyPanelLayout.setVerticalGroup(
            proxyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(proxyPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(useProxyCheckBox)
                .addGap(6, 6, 6)
                .addComponent(httpProxyRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(socksRadioButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(proxyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(proxyHostLabel)
                    .addComponent(portTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(proxyHostTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(portLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(proxyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(noProxyForLabel)
                    .addComponent(noProxyForTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(exampleLabel)
                .addGap(10, 10, 10)
                .addComponent(proxyNeedAuthCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(proxyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(loginTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(loginLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(proxyPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(85, 85, 85))
        );

        jTabbedPane1.addTab(bundle.getString("proxyPanel.title"), proxyPanel); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 440, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
    encodingListModel.remove(encodingsList.getSelectedIndex());
    encodingsList.setSelectedIndex(encodingsList.getSelectedIndex()-1);
    encodingsListValueChanged(null);
}//GEN-LAST:event_removeButtonActionPerformed

private void moveDownButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveDownButtonActionPerformed
    encodingListModel.moveDown(encodingsList.getSelectedIndex());
    encodingsList.setSelectedIndex(encodingsList.getSelectedIndex()+1);
}//GEN-LAST:event_moveDownButtonActionPerformed

private void moveUpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moveUpButtonActionPerformed
    encodingListModel.moveUp(encodingsList.getSelectedIndex());
    encodingsList.setSelectedIndex(encodingsList.getSelectedIndex()-1);
}//GEN-LAST:event_moveUpButtonActionPerformed

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
    encodingListModel.add(encodingComboBox.getSelectedItem().toString());
    encodingsList.setSelectedIndex(encodingListModel.getSize()-1);
}//GEN-LAST:event_addButtonActionPerformed

private void encodingComboBoxItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_encodingComboBoxItemStateChanged
    addButton.setEnabled(encodingComboBox.getSelectedIndex() != 0);
}//GEN-LAST:event_encodingComboBoxItemStateChanged

private void encodingsListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_encodingsListValueChanged
    int index = encodingsList.getSelectedIndex();
    if (index == 0) {
        moveUpButton.setEnabled(false);
    }else{
        moveUpButton.setEnabled(true);
    }
    if (index >= encodingListModel.getSize()-1){
        moveDownButton.setEnabled(false);
    }else{
        moveDownButton.setEnabled(true);
    }
    if (encodingListModel.getSize()<=1){
        removeButton.setEnabled(false);
    }else{
        removeButton.setEnabled(true);
    }
    
}//GEN-LAST:event_encodingsListValueChanged

    private void useCustomAgentCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_useCustomAgentCheckBoxActionPerformed
        customAgentField.setEnabled(useCustomAgentCheckBox.isSelected());
    }//GEN-LAST:event_useCustomAgentCheckBoxActionPerformed
    
    private void proxyNeedAuthCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_proxyNeedAuthCheckBoxActionPerformed
        boolean status = proxyNeedAuthCheckBox.isSelected();
        loginTextField.setEnabled(status);
        passwordField.setEnabled(status);
    }//GEN-LAST:event_proxyNeedAuthCheckBoxActionPerformed
    
    private void useProxyCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_useProxyCheckBoxActionPerformed
        boolean status = useProxyCheckBox.isSelected();
        proxyHostTextField.setEnabled(status);
        portTextField.setEnabled(status);
        noProxyForTextField.setEnabled(status);
        httpProxyRadioButton.setEnabled(status);
        socksRadioButton.setEnabled(status);
        proxyNeedAuthCheckBox.setEnabled(status);
        if (proxyNeedAuthCheckBox.isSelected()) {
            loginTextField.setEnabled(status);
            passwordField.setEnabled(status);
        }
    }//GEN-LAST:event_useProxyCheckBoxActionPerformed
    
    public void applyAction() {
        netSettingsWriter = Settings.getEditableNetworkSettings();
        saveProxySettings();
        saveFTPSettings();
        saveHTTPSettings();
        saveGeneralSettings();
        Settings.saveNetworkSettings();
    }
    
    public void okAction() {
        applyAction();
    }
    
    // <editor-fold defaultstate="collapsed" desc=" Internal methods for reading and saving settings ">
    
    private void readProxySettings() {
        if (netSettings.isProxyEnabled()){
            useProxyCheckBox.setSelected(true);
            proxyHostTextField.setEnabled(true);
            portTextField.setEnabled(true);
            noProxyForTextField.setEnabled(true);
            httpProxyRadioButton.setEnabled(true);
            socksRadioButton.setEnabled(true);
            proxyNeedAuthCheckBox.setEnabled(true);
            if (netSettings.isProxyNeedAuthorization()) {
                proxyNeedAuthCheckBox.setSelected(true);
                loginTextField.setEnabled(true);
                passwordField.setEnabled(true);
            }
        }
        
        
        if (netSettings.getProxyType() == Constants.PROXY_TYPE_SOCKS) {
            socksRadioButton.setSelected(true);
        }else {
            httpProxyRadioButton.setSelected(true);
        }
        
        proxyHostTextField.setText(netSettings.getProxyHost());
        if (netSettings.getProxyPort() > 0) {
            portTextField.setText(Integer.toString(netSettings.getProxyPort()));
        }else {
            portTextField.setText("");
        }
        noProxyForTextField.setText(netSettings.getNoProxyFor());
        loginTextField.setText(netSettings.getProxyLogin());
        passwordField.setText(netSettings.getProxyPassword());
    }
    
    private void saveProxySettings() {
        netSettingsWriter.setProxyEnabled(useProxyCheckBox.isSelected());
        if (socksRadioButton.isSelected()) {
            netSettingsWriter.setProxyType(Constants.PROXY_TYPE_SOCKS);
        }else {
            netSettingsWriter.setProxyType(Constants.PROXY_TYPE_HTTP);
        }
        
        netSettingsWriter.setProxyHost(proxyHostTextField.getText());
        if(proxyHostTextField.getText().length() > 0) {
            netSettingsWriter.setProxyPort(Integer.parseInt(portTextField.getText()));
        }else {
            netSettingsWriter.setProxyPort(0);
        }
        netSettingsWriter.setNoProxyFor(noProxyForTextField.getText());
        netSettingsWriter.setProxyNeedAuthorization(proxyNeedAuthCheckBox.isSelected());
        netSettingsWriter.setProxyLogin(loginTextField.getText());
        netSettingsWriter.setProxyPassword(new String(passwordField.getPassword()));
    }
    
    private void readFTPSettings() {
        dataTimeoutSpinner.setValue(netSettings.getFtpDataTimeout()/1000);
        
        if (netSettings.getFtpModeType() == Constants.FTP_MODE_ACTIVE) {
            activeModeRadioButton.setSelected(true);
        }else {
            if (netSettings.getFtpModeType() == Constants.FTP_MODE_PASSIVE) {
                passiveModeRadioButton.setSelected(true);
            }
        }
        
        if (netSettings.getFtpTransferType() == Constants.FTP_TRANSFER_BINARY) {
            binaryTypeRadioButton.setSelected(true);
        }else{
            if (netSettings.getFtpTransferType() == Constants.FTP_TRANSFER_BINARY_IMAGE) {
                binaryImageTypeRadioButton.setSelected(true);
            }else{
                if (netSettings.getFtpTransferType() == Constants.FTP_TRANSFER_ASCII) {
                    asciiTypeRadioButton.setSelected(true);
                }else {
                    if (netSettings.getFtpTransferType() == Constants.FTP_TRANSFER_EBCDIC) {
                        ebcdicTypeRadioButton.setSelected(true);
                    }
                }
            }
        }
        
        tcpNoDelayCheckBox.setSelected(netSettings.isFtpTcpNoDelay());
    }
    
    private void saveFTPSettings() {
        netSettingsWriter.setFtpDataTimeout((Integer)dataTimeoutSpinner.getValue()*1000);
        
        if (activeModeRadioButton.isSelected()) {
            netSettingsWriter.setFtpModeType(Constants.FTP_MODE_ACTIVE);
        }else {
            if (passiveModeRadioButton.isSelected()) {
                netSettingsWriter.setFtpModeType(Constants.FTP_MODE_PASSIVE);
            }
        }
        
        if (binaryTypeRadioButton.isSelected()) {
            netSettingsWriter.setFtpTransferType(Constants.FTP_TRANSFER_BINARY);
        }else{
            if (binaryImageTypeRadioButton.isSelected()) {
                netSettingsWriter.setFtpTransferType(Constants.FTP_TRANSFER_BINARY_IMAGE);
            }else{
                if (asciiTypeRadioButton.isSelected()) {
                    netSettingsWriter.setFtpTransferType(Constants.FTP_TRANSFER_ASCII);
                }else {
                    if (ebcdicTypeRadioButton.isSelected()) {
                        netSettingsWriter.setFtpTransferType(Constants.FTP_TRANSFER_EBCDIC);
                    }
                }
            }
        }
        
        netSettingsWriter.setFtpTcpNoDelay(tcpNoDelayCheckBox.isSelected());
    }
    
    private void readHTTPSettings() {
        if (netSettings.isUseCustomAgent()) {
            useCustomAgentCheckBox.setSelected(true);
            customAgentField.setEnabled(true);
            customAgentField.setText(netSettings.getUserAgent());
        }else {
            String agent = netSettings.getUserAgent();
            for (int i=0; i < userAgentComboBox.getItemCount(); i++) {
                if (((UserAgent)userAgentComboBox.getItemAt(i)).getValue().equals(agent)) {
                    userAgentComboBox.setSelectedIndex(i);
                    break;
                }
            }
        }
        
        timeoutSpinner.setValue(netSettings.getHttpTimeout()/1000);
    }
    
    private void saveHTTPSettings() {
        if(useCustomAgentCheckBox.isSelected()) {
            netSettingsWriter.setUseCustomAgent(true);
            netSettingsWriter.setUserAgent(customAgentField.getText());
            Mediator.getMediator().getHttpClient().getParams().setParameter(HttpMethodParams.USER_AGENT, netSettings.getUserAgent());
        }else {
            netSettingsWriter.setUseCustomAgent(false);
            netSettingsWriter.setUserAgent(((UserAgent)userAgentComboBox.getSelectedItem()).getValue());
        }
        
        netSettingsWriter.setHttpTimeout((Integer)timeoutSpinner.getValue()*1000);
    }
    
    public void readGeneralSettings() {
        simDownloadsSpinner.setValue(netSettings.getMaxWorkingThreads());
        bufferSpinner.setValue(netSettings.getDownloadBufferSize() / 1024);
        encodingListModel.setData(netSettings.getCharsetList());
    }
    
    public void saveGeneralSettings() {
        netSettingsWriter.setMaxWorkingThreads((Integer)simDownloadsSpinner.getValue());
        // apply this settings to http pool:
        HttpConnectionManagerParams params = ((MultiThreadedHttpConnectionManager)Mediator.getMediator().getHttpClient().getHttpConnectionManager()).getParams();
        params.setDefaultMaxConnectionsPerHost(netSettings.getMaxWorkingThreads());
        params.setMaxTotalConnections(netSettings.getMaxWorkingThreads());
        ThreadsMap.setMaxWorkingThreads(netSettings.getMaxWorkingThreads());
        
        netSettingsWriter.setDownloadBufferSize((Integer)bufferSpinner.getValue() * 1024);
        netSettingsWriter.setCharsetList(encodingListModel.getData());
    }
    
    //</editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc=" Encoding List Model ">
    
    private static class EncodingListModel extends AbstractListModel{
        
        private ArrayList<String> data= new ArrayList<String>();
        
        public int getSize() {
            return data.size();
        }
        
        public Object getElementAt(int index) {
            return data.get(index);
        }
        
        public void remove(int index) {
            data.remove(index);
            fireContentsChanged(this, 0, data.size());
        }
        
        public void add(String item){
            data.add(item);
            fireContentsChanged(this, 0, data.size());
        }
        
        public void moveUp(int index){
            Collections.swap(data, index, index-1);
            fireContentsChanged(this, 0, data.size());
        }
        
        public void moveDown(int index){
            Collections.swap(data, index, index+1);
            fireContentsChanged(this, 0, data.size());
        }
        
        public ArrayList<String> getData(){
            return data;
        }
        
        public void setData(ArrayList<String> data){
            this.data = data;
            fireContentsChanged(this, 0, data.size());
        }
    }
    
    // </editor-fold>
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton activeModeRadioButton;
    private javax.swing.JButton addButton;
    private javax.swing.JRadioButton asciiTypeRadioButton;
    private javax.swing.JRadioButton binaryImageTypeRadioButton;
    private javax.swing.JRadioButton binaryTypeRadioButton;
    private javax.swing.JLabel bufferSizeLabel;
    private javax.swing.JSpinner bufferSpinner;
    private javax.swing.JTextField customAgentField;
    private javax.swing.JLabel dataTimeoutLabel;
    private javax.swing.JSpinner dataTimeoutSpinner;
    private javax.swing.JRadioButton ebcdicTypeRadioButton;
    private javax.swing.JComboBox encodingComboBox;
    private javax.swing.JLabel encodingLabel;
    private javax.swing.JList encodingsList;
    private javax.swing.JLabel exampleLabel;
    private javax.swing.ButtonGroup ftpModeGroup;
    private javax.swing.JPanel ftpPanel;
    private javax.swing.ButtonGroup ftpTransTypeGroup;
    private javax.swing.JPanel generalPanel;
    private javax.swing.JPanel httpPanel;
    private javax.swing.JRadioButton httpProxyRadioButton;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel loginLabel;
    private javax.swing.JTextField loginTextField;
    private javax.swing.JPanel miscPanel;
    private javax.swing.JPanel modePanel;
    private javax.swing.JButton moveDownButton;
    private javax.swing.JButton moveUpButton;
    private javax.swing.JLabel noProxyForLabel;
    private javax.swing.JTextField noProxyForTextField;
    private javax.swing.JRadioButton passiveModeRadioButton;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JLabel portLabel;
    private javax.swing.JTextField portTextField;
    private javax.swing.ButtonGroup proxyGroup;
    private javax.swing.JLabel proxyHostLabel;
    private javax.swing.JTextField proxyHostTextField;
    private javax.swing.JCheckBox proxyNeedAuthCheckBox;
    private javax.swing.JPanel proxyPanel;
    private javax.swing.JButton removeButton;
    private javax.swing.JLabel simDownloadsLabel;
    private javax.swing.JSpinner simDownloadsSpinner;
    private javax.swing.JRadioButton socksRadioButton;
    private javax.swing.JCheckBox tcpNoDelayCheckBox;
    private javax.swing.JLabel timeoutLabel;
    private javax.swing.JSpinner timeoutSpinner;
    private javax.swing.JPanel transferTypePanel;
    private javax.swing.JCheckBox useCustomAgentCheckBox;
    private javax.swing.JCheckBox useProxyCheckBox;
    private javax.swing.JComboBox userAgentComboBox;
    private javax.swing.JLabel userAgentLabel;
    // End of variables declaration//GEN-END:variables
    
}
